package com.it.service.impl;

import com.it.mapper.MenuRoleMapper;
import com.it.pojo.MenuExample;
import com.it.pojo.MenuRole;
import com.it.pojo.MenuRoleExample;
import com.it.service.MenuRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.management.MemoryUsage;
import java.util.List;
import java.util.UUID;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/19 16:32
 */
@Service
@Transactional
public class MenuRoleServiceImpl implements MenuRoleService {

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    /*根据menuId和roleId修改一条记录  移除权限*/
    @Override
    public int updateByMidAndRid(String menuId, String roleId) {
        MenuRole menuRole = menuRoleMapper.findByMidAndRid(menuId, roleId);
        menuRole.setMenuRoleDel(1);
        return menuRoleMapper.updateByPrimaryKeySelective(menuRole);
    }

    /*根据menuId和roleId修改一条记录   添加权限*/
    @Override
    public int updateAddMidAndRid(String menuId, String roleId) {
        MenuRoleExample example = new MenuRoleExample();
        List<MenuRole> menuRoleList = menuRoleMapper.selectByExample(example);

        MenuRole menuRole = menuRoleMapper.findByMidAndRid(menuId, roleId);
        /*如果menuRole存在说明表里面有，直接修改del状态，如果不存在，则新添加一条记录即可*/
        if (menuRole==null){
            menuRole = new MenuRole();
            menuRole.setMenuId(menuId);
            menuRole.setRoleId(roleId);
            menuRole.setMenuRoleId(String.valueOf(menuRoleList.size()+1));
            menuRole.setMenuRoleDel(0);
            menuRoleMapper.insertSelective(menuRole);
        }else{
            menuRole.setMenuRoleDel(0);
        }
        return menuRoleMapper.updateByPrimaryKeySelective(menuRole);
    }
}
